package com.dmp.common.date;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Date工具类
 * @author hutao
 *
 */
public class DateUtil {
	
	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

	public static Timestamp  utc2Timestamp(String str){
		 Date today = new Date(Long.valueOf(str));
		 return new Timestamp(today.getTime());
	}

	public static Timestamp date2Timestamp(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			return new Timestamp(sdf.parse(datetime).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}
	
	/**
	 * 
	 * @Title: date2String 
	 * @Description: 根据时间格式转换时间
	 * @date 2015年11月5日 下午5:52:49 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2String(Date date ,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String result = "";
		try{
			result = sdf.format(date);
		}catch(Exception e){
			
		}
		return result;
		
	}
	
	public static Date string2Date(String dateStr,String format) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date  = sdf.parse(dateStr);
		return date;
	}
	
	public static String formatDate(String dateStr,String oldFormat,String newFormat) throws Exception{
		Date date = string2Date(dateStr,oldFormat);
		return date2String(date,newFormat);
	}
}
